<?php
require_once 'models/model_manager.php';
require_once 'models/camp.php';
require_once 'models/entite_historique_manager.php';

class camp_manager extends Manager
{
	public function get_all_by_bataille($idbataille) {
		$camps = array();
		$query = "SELECT DISTINCT idCamp FROM combat WHERE idBataille = ". $idbataille;
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		
		while ($data = oci_fetch_assoc($result)) {
			$query2 = "SELECT * FROM camp WHERE idCamp = ". $data["IDCAMP"];
			$result2 = oci_parse($this->_db, $query2);
			oci_execute($result2);
			
			while ($data2 = oci_fetch_assoc($result2)) {
				$camp = new camp($data2);
				$camp->set_entites($this->set_entites($camp->get_idcamp(),$idbataille));
				$camp->set_morts($this->get_nb_mort($camp->get_idcamp(),$idbataille));
				$camp->set_blesses($this->get_nb_blesses($camp->get_idcamp(),$idbataille));
				$camps[] = $camp;
			}
		}
		return $camps;
	}

	public function set_entites($idcamp,$idbataille) {
		$entites = array();
		$query = "SELECT idEntite FROM combat WHERE idBataille = ". $idbataille." AND idCamp = ".$idcamp;
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		$e = new entite_historique_manager($this->_db);
		while ($data = oci_fetch_assoc($result)) {
			$entites[] = $e->get_by_id($data["IDENTITE"]);
		}
		return $entites;
	}

	public function get_nb_mort($idcamp,$idbataille) {
		$query = "SELECT SUM(morts) FROM combat WHERE idBataille = ". $idbataille." AND idCamp = ".$idcamp;
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		while ($data = oci_fetch_assoc($result)) {
			return $data["SUM(MORTS)"];
		}
	}

	public function get_nb_blesses($idcamp,$idbataille) {
		$query = "SELECT SUM(blesses) FROM combat WHERE idBataille = ". $idbataille." AND idCamp = ".$idcamp;
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		while ($data = oci_fetch_assoc($result)) {
			return $data["SUM(BLESSES)"];
		}
	}
	
	public function max_id() {
		$query = "SELECT max(idCamp) FROM camp";
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		$r = oci_fetch_row($result);
		return $r[0];
	}

	public function add_or_update($e,$idbataille){
		echo "bataille";
		$query = "SELECT COUNT(*) FROM camp WHERE idCamp=".$e->get_idcamp();
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		$d = oci_fetch_row($result);
		if($d[0] == 0)$this->add($e,$idbataille);
		else{
			$query = "UPDATE camp SET nomCamp='".$e->get_nomcamp()."', wikiCamp='".$e->get_wikicamp()."'";
			$result = oci_parse($this->_db, $query);
			echo "<br>" . $query . "<br><br>";
			oci_execute($result);
			$r = new bataille_manager($this->_db);
			$this->delete_combat($e->get_entites(),$e->get_idcamp(),$idbataille);
			foreach($e->get_entites() as $ent){
				$query = "SELECT COUNT(*) FROM combat WHERE idEntite=".$ent." AND idCamp=".$e->get_idcamp()." AND idBataille=".$idbataille;
				echo $query;
				$result = oci_parse($this->_db, $query);
				oci_execute($result);
				$d = oci_fetch_row($result);
				$morts = ($e->get_morts())/(count($e->get_entites()));
				$blesses = $e->get_blesses()/count($e->get_entites());
				if($d[0] == 0){
					$query = "INSERT INTO combat(idEntite, idCamp, idBataille, morts, blesses)
					VALUES (" . $ent. "," . $e->get_idcamp() . "," . $idbataille . "," . $morts ."," . $blesses . ")";
					$result = oci_parse($this->_db, $query);
					echo "<br>" . $query . "<br><br>";
					oci_execute($result);
				}
				else{
					/*$query = "UPDATE combat SET morts=".$morts.",blesses=".$blesses." WHERE idEntite=".$ent." AND idCamp=".$e->get_idcamp()." AND idBataille=".$idbataille;
					$result = oci_parse($this->_db, $query);
					oci_execute($result);*/
				}
			}
		}
	}	

	public function add($e,$idbataille) {
		$query = "INSERT INTO camp(idCamp, nomCamp, wikiCamp)
			VALUES (" . $e->get_idcamp() . ",'" . $e->get_nomcamp() . "','" . $e->get_wikicamp() . "')";
		$result = oci_parse($this->_db, $query);
		echo "<br>" . $query . "<br><br>";
		oci_execute($result);
		foreach($e->get_entites() as $ent){
			$morts = ($e->get_morts())/(count($e->get_entites()));
			$blesses = $e->get_blesses()/count($e->get_entites());
			$query = "INSERT INTO combat(idEntite, idCamp, idBataille, morts, blesses)
			VALUES (" . $ent. "," . $e->get_idcamp() . "," . $idbataille . "," . $morts ."," . $blesses . ")";
			$result = oci_parse($this->_db, $query);
			echo "<br>" . $query . "<br><br>";
			oci_execute($result);
		}
	}

	public function delete($e){
		$query = "DELETE FROM camp WHERE idCamp =" . $e->get_idcamp();
		$result = oci_parse($this->_db, $query);
		echo "<br>" . $query . "<br><br>";
		oci_execute($result);
	}

	public function delete_by_id($e){
		$query = "DELETE FROM camp WHERE idCamp =" . $e;
		$result = oci_parse($this->_db, $query);
		echo "<br>" . $query . "<br><br>";
		oci_execute($result);
	}

	public function delete_combat($idEntite,$idCamp,$idBataille){
		$ent = implode(",",$idEntite);
		if($ent != "") {
			$query = "DELETE FROM combat WHERE idEntite NOT IN (".$ent.") AND idCamp=".$idCamp." AND idBataille=".$idBataille;
			$result = oci_parse($this->_db, $query);
			echo "<br>" . $query . "<br><br>";
			oci_execute($result);
		}
	}
}
?>
